package com.naihe.anderson.sms.util;

import java.io.File;
import java.io.OutputStream;
import java.io.OutputStreamWriter;
import java.io.StringWriter;
import java.util.ArrayList;
import java.util.List;

import javax.xml.parsers.SAXParser;
import javax.xml.parsers.SAXParserFactory;

import org.xmlpull.v1.XmlSerializer;

import android.content.Context;
import android.util.Xml;

import com.naihe.anderson.sms.entity.SignatureMsg;

public class SignatureXmlUtil {

	public static final String filePath = "signatureMsgInfo.xml";
	public static List<SignatureMsg> getSignatureMsgByXml(Context context){
		File f = context.getFileStreamPath(filePath);  
        if(f.exists())
        {  
            SAXParserFactory factory = SAXParserFactory.newInstance();  
            try {  
                SAXParser sp = factory.newSAXParser();  
                SignatureXmlHandler handler = new SignatureXmlHandler();  
                sp.parse(f, handler);
                
                return handler.getSignatureMsgList();
            } catch(Exception e) {  
            	FileUtil.addLog("SignatureXmlUtil::getUserInfosByXml", SignatureXmlUtil.class, "SignatureXmlUtil", e);
            }  
        }  
        
        return new ArrayList<SignatureMsg>();
	}
	
	public static void saveSignatureMsg(Context context, List<SignatureMsg> signatureMsgList){
		if(signatureMsgList == null)
			return;
		
		XmlSerializer serializer = Xml.newSerializer();
		StringWriter writer = new StringWriter();
		try{
			serializer.setOutput(writer);
			// <?xml version=”1.0″ encoding=”UTF-8″ standalone=”yes”?>
			serializer.startDocument("UTF-8",true);
		
			// <SignatureMsgss>
			serializer.startTag("","SignatureMsgList");

			for(SignatureMsg entitySignatureMsg: signatureMsgList){
				//<SignatureMsg signatureMsgId=”0000000″>
				serializer.startTag("","SignatureMsg");
				
				serializer.attribute("","signatureMsgId", entitySignatureMsg.getSignatureMsgId()+"");

				// <bodyMsg>qingzhi001</bodyMsg>
				serializer.startTag("", "bodyMsg");
				serializer.text(entitySignatureMsg.getBodyMsg());
				serializer.endTag("", "bodyMsg");
				
				// <contactNumbers>wuqh</contactNumbers>
				serializer.startTag("", "contactNumbers");
				serializer.text(entitySignatureMsg.getContactNumbersForSave());
				serializer.endTag("", "contactNumbers");
				
				// <signatureMsgStatut>wuqh</signatureMsgStatut>
				serializer.startTag("", "signatureMsgStatut");
				serializer.text(entitySignatureMsg.getSignatureMsgStatut()+"");
				serializer.endTag("", "signatureMsgStatut");
				
				// <signatureMsgTime>wuqh</signatureMsgTime>
				serializer.startTag("", "signatureMsgTime");
				serializer.text(entitySignatureMsg.getSignatureMsgTime()+"");
				serializer.endTag("", "signatureMsgTime");
				
				serializer.endTag("","SignatureMsg");
			}
			
			// </SignatureMsgList>
			serializer.endTag("", "SignatureMsgList");
			serializer.endDocument();
			write(writer.toString(), context);
		}catch (Exception e) {
			
		}
	}
	
	private static boolean write(String txt,Context context)
	{
		try
		{
			OutputStream os = context.openFileOutput(filePath, Context.MODE_PRIVATE);
			OutputStreamWriter osw=new OutputStreamWriter(os);
			osw.write(txt);
			osw.close();
			os.close();
		}
		catch(Exception e)
		{
			return false;
		}
		return true;
	}
	
}
